﻿@{
    ViewData["Title"] = "人脸检测";
}
<div class="layui-layout layui-layout-admin">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">待检测图片</label>
                    <div class="layui-input-inline">
                        <input type="checkbox" name="BBB" lay-text="截取人脸小图" checked>
                    </div>
                    <div class="layui-input-inline">
                        <button type="button" class="layui-btn" id="ID-upload-demo-btn">
                            <i class="layui-icon layui-icon-upload"></i> 选择并上传...
                        </button>
                    </div>
                </div>
            </form>
            <hr>
            <div class="layui-bg-gray">
                <div class="layui-row">
                    <div class="layui-col-xs12">
                        <div class="layui-card">
                            <div class="layui-card-header" id="num">人脸数量</div>
                            <div class="layui-card-body" id="smallpic">
                                缩略图
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row layui-col-space15">
                    <div class="layui-col-md6">
                        <div class="layui-card">
                            <div class="layui-card-header">原始画面</div>
                            <div class="layui-card-body">
                                <img class="layui-upload-img" id="ID-upload-demo-img" style="width: 100%; height: 392px;">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <div class="layui-card">
                            <div class="layui-card-header">输出</div>
                            <div class="layui-card-body">
                                <img class="layui-upload-img" id="ID-upload-demo-img1" style="width: 100%; height: 392px;">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@section scripts {
    <script type="text/javascript">
        var app = {};
        layui.use(['tools', 'toast', 'http'], function () {
            var $ = layui.$, upload = layui.upload, form = layui.form,
                http = layui.http,
                toast = layui.toast,
                tools = layui.tools;

            app.init = function () {
                app.uploadIns = upload.render({
                    elem: '#ID-upload-demo-btn',
                    auto: false,
                    accept: 'file',
                    choose: function (obj) {
                        // 预读本地文件示例，不支持ie8
                        obj.preview(function (index, file, result) {
                            $('#ID-upload-demo-img').attr('src', result); // 图片链接（base64）
                            //
                            var data = {
                                "image_type": result.split(',')[1],
                                "display_corp_image": 1
                            }
                           app.upload(data);
                        });
                    }
                });
            }();
            //
            app.upload = function (data) {
                http.post('/api/face/detect', data).then((res) => {
                    $('#num').html('人脸数量：' + res.result.face_num);
                    var imgs = '';
                    $.each(res.result.face_list, function (index, val) {
                        imgs += '<img src="data:image/png;base64,' + val.corp_image_base64 + '">';
                    });
                    $('#smallpic').html(imgs);
                    $('#ID-upload-demo-img1').attr('src', 'data:image/png;base64,' + res.result.image_base64);
                })
            }
        })
    </script>
}
